package cn.cn77.futures.utils;

import android.content.Context;
import android.content.SharedPreferences;

import cn.cn77.futures.BaseActivity;

public class UserSharedPreference {

	private final static BaseActivity activity = new BaseActivity();
	/**
	 * 用户的ID
	 */
	public final static String USER_ID = "uid";
	/**
	 * 用户的电话字段
	 */
	public final static String MOBILE = "mobile";
	/**
	 * 最后一次登录的时间
	 */
	public final static String TimeStamp = "lasttime";
	/**
	 * 登录密码
	 */
	public final static String Password = "password";

	/**
	 * 保存用户信息
	 * 
	 * @param paramsName
	 *            字段名
	 * @param value
	 *            参数值
	 */
	public static void saveParams(Context context, String paramsName, String value) {
		SharedPreferences preferences = context.getSharedPreferences(
				Contants.PROPERTIES_FILE, context.MODE_APPEND);
		SharedPreferences.Editor editor = preferences.edit();
		String idString = getParams(paramsName, context);
		if (idString != null && !"".equals(idString)) {
			editor.putString(paramsName, "");
		}
		editor.putString(paramsName, value);
		editor.commit();
	}

	/**
	 * 获取用户的ID
	 * 
	 * @param paramsName
	 *            字段名
	 * @param context 
	 * @return 返回用户的ID
	 */
	public static String getParams(String paramsName, Context context) {
		SharedPreferences preferences = context.getSharedPreferences(
				Contants.PROPERTIES_FILE, activity.MODE_PRIVATE);
		if (preferences.contains(paramsName)) {
			String id = preferences.getString(paramsName, "");
			return id;
		}
		return null;
	}

	/**
	 * 保存用户信息
	 * 
	 * @param paramsNameAndValue
	 */
	public static void saveMessage(Context context, String paramsNameAndValue[][]) {
		SharedPreferences preferences = context.getSharedPreferences(
				Contants.PROPERTIES_FILE, context.MODE_APPEND);
		SharedPreferences.Editor editor = preferences.edit();
		for (int i = 0; i < paramsNameAndValue.length; i++) {
			String data = getParams(paramsNameAndValue[i][0], context);
			if (data != null && !"".equals(data)) {
				editor.putString(paramsNameAndValue[i][1], "");
			}
			editor.putString(paramsNameAndValue[i][0], paramsNameAndValue[i][0]);
		}
		editor.commit();
	}

}
